package my.cn.bdqn.life.service;

import java.util.Map;

import my.cn.bdqn.life.HomeActivity;
import my.cn.bdqn.life.LoginActivity;
import my.cn.bdqn.life.R;
import my.cn.bdqn.life.RegActivity;
import my.cn.bdqn.life.TabMovieActivity;
import my.cn.bdqn.life.myconst.CMD;
import my.cn.bdqn.life.net.URLProtocol;
import my.cn.bdqn.life.net.impl.URLProtocolImpl;

import org.json.JSONObject;

import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

public class FreeService extends Service {
	public static final int REG_INTENT = 0;
	public static final int ASK_SUCCESS = 1;
	public static final int ASK_FAILED = 2;
	public static final int RES_SUCCESS = 0;
	public static final int RES_FAILED = 1;
	public static final int LOGIN_SUCCESS = 3;
	public static final int LOGIN_FAILED = 4;
	public static final int G_MOVIE_SUCCESS=1010;
	public static final int G_MOVIE_FAILED=1011;
	public static int cmd = -1;
	public static JSONObject back=null;
	public static Map<String,String>map;

	public Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			//һ��ģ��һ��case
			switch (msg.what) {
			case ASK_SUCCESS:
				  reg();
				break;
			case ASK_FAILED:
				Toast.makeText(FreeService.this,R.string.regiest_fail, 1).show();
				break;
			case G_MOVIE_SUCCESS:
				showMovieList();
				break;
			case G_MOVIE_FAILED:
				HomeActivity.progress.dismiss();
				Toast.makeText(FreeService.this,"�����쳣��������ʧ��", 1).show();
				break;
			}
		}
	};
	private void showMovieList(){
		HomeActivity.progress.dismiss();
		Intent intent = new Intent(FreeService.this,
			TabMovieActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		FreeService.this.startActivity(intent);
	}
	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		Log.i("ddddddd", "service is start");
	}

	@Override
	public void onStart(Intent intent, int startId) {
		super.onStart(intent, startId);
		//һ��ģ��һ��case
		switch (intent.getIntExtra("cmd", -1)) {
		case CMD.REG:
			cmd = CMD.REG;
			map=RegActivity.map;
			getData();
			break;
		case CMD.LOGIN:
			cmd = CMD.LOGIN;
//			getData();
			break;
		case CMD.MOVIELIST:
			cmd=CMD.MOVIELIST;
			map=HomeActivity.map;
			getData();
			break;

		}

	}

	private void getData() {		
		new Thread() {
			@Override
			public void run() {
				try {
					URLProtocol up = new URLProtocolImpl();
					back = up.getData(map);
					int code = back.getInt("code");					
					checkCMD(code);
				} catch (Exception e) {
					checkCMD(RES_FAILED);
				}
			}
		}.start();
	}//getData
	//Ҫ��������
	private void checkCMD(int code){
		switch(cmd){
		case CMD.REG:
			checkCode(code,ASK_SUCCESS,ASK_FAILED);
			break;
		case CMD.LOGIN:
			checkCode(code,LOGIN_SUCCESS,LOGIN_FAILED);
			break;
		case CMD.MOVIELIST:
			checkCode(code,G_MOVIE_SUCCESS,G_MOVIE_FAILED);
			break;
		}
	}//checkCMD
	private void checkCode(int code,int success,int error){
		switch (code) {
		case RES_SUCCESS:
			handler.sendEmptyMessage(success);
			break;
		case RES_FAILED:
			handler.sendEmptyMessage(error);
			break;
		}
	}//checkCode
	//��ģ��ķ���
	private void reg(){
		Toast.makeText(FreeService.this, R.string.regiest_success, 1).show();
		Map<String,String> map=RegActivity.map;
		String name=map.get("name");
		String password=map.get("password");
		String uid=null;
		try{uid=back.getString("uid");}catch(Exception e){}
		SharedPreferences sp=getSharedPreferences("freelife",MODE_PRIVATE);
		SharedPreferences.Editor editor=sp.edit();
		editor.putString("name", name);
		editor.putString("password", password);
		editor.putString("uid", uid);
		editor.commit();
		RegActivity.currentReg.finish();
		Intent intent = new Intent(FreeService.this,
				LoginActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		FreeService.this.startActivity(intent);
	}
}









